package com.sgb.kjwl.view.ui.fragment.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentChildLeaseAskBinding;
import com.sgb.kjwl.di.home.DaggerHomeFragComponent;
import com.sgb.kjwl.di.home.HomeFragModule;
import com.sgb.kjwl.model.entity.lease.AskLeaseSheBeiEntity;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.view.ui.adapter.machine.AskLeaseAdapter;
import com.sgb.kjwl.viewmodel.home.ChildLeaseAskFragmentModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 租赁首页推荐-求租大厅
 */
@Deprecated
public class ChildLeaseAskFragment extends AppLazyFragment {

    @Inject
    ChildLeaseAskFragmentModel askLeaseFragModel;
    private FragmentChildLeaseAskBinding binding;
    private List<DictEntity3> classDatas = new ArrayList<>();
    private AskLeaseAdapter askLeaseAdapter;
    private int page = 1;
    private boolean isLoadMore = false;
    private int type;
    private String classNo = "";

    public static ChildLeaseAskFragment newInstance(int type) {
        ChildLeaseAskFragment fragment = new ChildLeaseAskFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerHomeFragComponent.builder()
                .appComponent(appComponent)
                .homeFragModule(new HomeFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_child_lease_ask, container, false);
        return binding.getRoot();
    }

    @Override
    protected void onLazyLoad() {
//        type = getArguments().getInt("type");
//        if (type != 0) {
//            binding.tabLayout.setVisibility(View.VISIBLE);
//            askLeaseFragModel.getFilterTypeModel(String.valueOf(type));
//        } else {
//            binding.tabLayout.setVisibility(View.GONE);
//        }
//        getData();
    }

    @Override
    protected void initView() {
//        initTabType();
//        initAdapter();
    }

    private void initAdapter() {
        if (askLeaseAdapter == null) {
            askLeaseAdapter = new AskLeaseAdapter(getActivity(), null);
        }
        binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0,
                getActivity().getResources().getDimension(R.dimen.dp2),
                getActivity().getResources().getColor(R.color.color_F5F5F5)));
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setAdapter(askLeaseAdapter);
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                isLoadMore = false;
                page = 1;
                getData();
            }

            @Override
            public void onLoadMore() {
                isLoadMore = true;
                page = page + 1;
                getData();
            }
        });

        askLeaseAdapter.setOnItemClick(new AdapterOnItemClick<AskLeaseSheBeiEntity>() {
            @Override
            public void onItemClick(AskLeaseSheBeiEntity tenderListEntity, int p) {
                if (!ClickUtils.isDoubleClick()) {
                    askLeaseFragModel.getH5Url(tenderListEntity.getType(), tenderListEntity.getId());
                }
            }
        });
    }

    private void initTabType() {
        //列表分类标题
        binding.tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                classNo = classDatas.get(tab.getPosition()).getId();
                isLoadMore = false;
                page = 1;
                getData();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void getData() {
        askLeaseFragModel.getDataList(String.valueOf(type-1), classNo, "", "", "", "1", page);
    }

    public void showList(List<AskLeaseSheBeiEntity> list) {
        binding.recyclerView.refreshComplete();
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (null != list && list.size() > 0) {
                askLeaseAdapter.addDatas(list);
            } else {
                binding.recyclerView.setLoadingMoreEnabled(false);
            }
            binding.recyclerView.setVisibility(View.VISIBLE);
        } else {
            binding.recyclerView.refreshComplete();
            if (null != list && list.size() > 0) {
                binding.recyclerView.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                askLeaseAdapter.setDatas(list);
                binding.recyclerView.setLoadingMoreEnabled(true);
            } else {
                askLeaseAdapter.setDatas(null);
                binding.recyclerView.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }


    public void showCategoryList(List<DictEntity3> list) {
        this.classDatas = list;
        binding.tabLayout.removeAllTabs();
        for (int i = 0; i < classDatas.size(); i++) {
            String tabName = classDatas.get(i).getName();
            if ("不限".equals(tabName)) {
                tabName = "全部";
            }
            binding.tabLayout.addTab(binding.tabLayout.newTab().setText(tabName));
        }

    }

}
